home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / clang / crobots.zip / RABBIT.R < prev    next >
Text File  |  1986-08-07  |  1KB  |  89 lines

  1. /* rabbit */
  2. /* rabbit runs around the field, randomly */
  3. /* and never fires;  use as a target */
  4.  
  5.  
  6. main()
  7. {
  8.  
  9.   while(1) {
  10.     go(rand(1000),rand(1000));  /* go somewhere in the field */
  11.   }
  12.     
  13. }  /* end of main */ 
  14.  
  15.  
  16.  
  17. /* go - go to the point specified */
  18.  
  19. go (dest_x, dest_y)
  20. int dest_x, dest_y;
  21. {
  22.   int course;
  23.  
  24.   course = plot_course(dest_x,dest_y);
  25.   drive(course,25);
  26.   while(distance(loc_x(),loc_y(),dest_x,dest_y) > 50)
  27.     ;
  28.   drive(course,0);
  29.   while (speed() > 0)
  30.     ;
  31. }
  32.  
  33. /* distance forumula */
  34.  
  35. distance(x1,y1,x2,y2)
  36. int x1;
  37. int y1;
  38. int x2;
  39. int y2;
  40. {
  41.   int x, y;
  42.  
  43.   x = x1 - x2;
  44.   y = y1 - y2;
  45.   d = sqrt((x*x) + (y*y));
  46.  
  47.   return(d);
  48. }
  49.  
  50. /* plot_course - figure out which heading to go */
  51.  
  52. plot_course(xx,yy)
  53. int xx, yy;
  54. {
  55.   int d;
  56.   int x,y;
  57.   int scale;
  58.   int curx, cury;
  59.  
  60.   scale = 100000;  /* scale for trig functions */
  61.  
  62.   curx = loc_x();
  63.   cury = loc_y();
  64.   x = curx - xx;
  65.   y = cury - yy;
  66.  
  67.   if (x == 0) {
  68.     if (yy > cury)
  69.       d = 90;
  70.     else
  71.       d = 270;
  72.   } else {
  73.     if (yy < cury) {
  74.       if (xx > curx)
  75.         d = 360 + atan((scale * y) / x);
  76.       else
  77.         d = 180 + atan((scale * y) / x);
  78.     } else {
  79.       if (xx > curx)
  80.         d = atan((scale * y) / x);
  81.       else
  82.         d = 180 + atan((scale * y) / x);
  83.     }
  84.   }
  85.   return (d);
  86. }
  87.     
  88. /* end of rabbit.r */
  89.